package fr.ajicommerce.managedBean;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import fr.ajicommerce.UserBackOffice;
import fr.ajicommerce.core.dto.UtilisateurBackOfficeDTO;
import fr.ajicommerce.core.entities.TypeUtilisateur;
import fr.ajicommerce.core.service.facade.Facade;
import fr.ajicommerce.core.service.interfaces.UtilisateurBackOfficeService;


@Component("loginBackOfficeBean")
@Scope("request")
public class LoginBackOfficeBean {

	@Autowired
	Facade facade;
	
	@Autowired
    private AuthenticationManager authenticationManager = null;
	
	private String login;
	private String mdp;
	private boolean loginFail;
	private boolean isConnect;
	
	
	public LoginBackOfficeBean() {
		this.setLoginFail(false);
	}
	
	public String connection()
	{
//		InitBase initBase = new InitBase();
//		initBase.init();
		System.out.println(authenticationManager.toString());
		
		FacesContext context = FacesContext.getCurrentInstance();
		UserBackOffice user = (UserBackOffice) context.getExternalContext().getSessionMap().get("user");
		if (user == null)
		{
			isConnect = false;
			 try {
		        	
		            Authentication request = new UsernamePasswordAuthenticationToken(login, mdp);
		            Authentication result = authenticationManager.authenticate(request);
		            SecurityContextHolder.getContext().setAuthentication(result);
		        } catch (AuthenticationException e) {
		            
		            loginFail = true;
		            System.out.println(e.getMessage());
		            return null;
		        }
			 
		        
			 isConnect = true;	
			 UtilisateurBackOfficeService service = facade.getUtilisateurBackOfficeService();
				
				UtilisateurBackOfficeDTO uDTO =  service.findUtilisateurBackOfficeByLogin(login);
				user = new UserBackOffice();
				user.setLogin(login);
				user.setTypeUser(uDTO.getMyTypeDUtilisateur());
				context.getExternalContext().getSessionMap().put("user", user);
				
				if (user.getTypeUser() == TypeUtilisateur.GESTIONNAIRE_UTILISATEUR)
				{
					return "/BackOffice/GestionUtilisateurs/gest_users.xhtml";
				}
				else if (user.getTypeUser() == TypeUtilisateur.GESTIONNAIRE_PRODUIT)
				{
					return "/BackOffice/GestionProduits/gest_produits.xhtml";
				}
				else
				{
					return "/BackOffice/GestionComptable/gest_factures.xhtml";
				}
			}
		return null;
			
			
	
	}
	
	public String logout(){
		FacesContext context = FacesContext.getCurrentInstance();
		
		
		context.getExternalContext().getSessionMap().clear();
		
        SecurityContextHolder.clearContext();
        
        
        return "/BackOffice/loginBackOffice.xhtml";
    }
	
	public Facade getFacade() {
		return facade;
	}
	public void setFacade(Facade facade) {
		this.facade = facade;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getMdp() {
		return mdp;
	}
	public void setMdp(String mdp) {
		this.mdp = mdp;
	}

	public boolean isLoginFail() {
		return loginFail;
	}

	public void setLoginFail(boolean loginFail) {
		this.loginFail = loginFail;
	}

	public boolean isConnect() {
		FacesContext context = FacesContext.getCurrentInstance();
		UserBackOffice user = (UserBackOffice) context.getExternalContext().getSessionMap().get("user");
		
		if (user != null)
			return true;
		return false;
	}

	public void setConnect(boolean isConnect) {
		this.isConnect = isConnect;
	}
	
}
